package ui;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Label;
import org.eclipse.wb.swt.SWTResourceManager;
import Music_Playing.Music_Playing;
import each_class.SongClass;
import ui.global.GraphicsUtil;



public class PlayList extends Composite implements Runnable{

	private int currentMusicIndex = -1;//当前播放的音乐序号
	private List<SongClass> playList;//播放列表在initial方法初始化
	private ScrolledView listView;//播放列表试图
	private Display display;
	//private Music_Playing  mp;
	private Label amountView;
	
	
	//初始化播放列表每次切换歌单时调用此方法从新初始化
	public void initail(List<SongClass> playList) {
		if (playList == null) {
			return;
		}
		this.playList = playList;
		currentMusicIndex = 0;
		newListView();
		amountView.setText(playList.size() + "首歌曲");
		for (SongClass music : playList) {
			new PlaylistItem(listView, music);
			listView.downcount += listView.scrollHeight;
		}
		listView.layout();
		play(currentMusicIndex);
	}
	
	public void deleteMusic(SongClass music) {
		int index = playList.indexOf(music);
		if (index == -1) {
			return;
		}
		if (index > currentMusicIndex) {
		} else if (index < currentMusicIndex) {
			--currentMusicIndex;
		}else {
			stop();
		}
		playList.remove(music);
		amountView.setText(playList.size() + "首歌曲");
	}
	
	public SongClass getCurrentMusic() {
		if (playList != null || currentMusicIndex != -1) {
			return playList.get(currentMusicIndex);
		}
		return null;
	}
	
	public void addMusic(SongClass music) {
		if (music == null) {
			return;
		}
		if (playList == null) {
			playList =new LinkedList<>();
			
		}
		int index;
		if((index = playList.indexOf(music)) != -1) {
			currentMusicIndex = index;
			play(index);
			return;
		}
		new PlaylistItem(listView, music);
		playList.add(music);
		amountView.setText(playList.size() + "首歌曲");
		listView.downcount += listView.scrollHeight;
		listView.layout();
		currentMusicIndex = playList.size() - 1;
		play(currentMusicIndex);
	}
	
	private void loadMusicInfo(SongClass music) {
		if (music == null) {
			return;
		}
		new Thread() {
			@Override
			public void run() {
				InputStream inputStream = ui.global.Operation.loadMusicCover(music);
				if (inputStream == null) {
					return;
				}
				Image musicCover = new Image(null, inputStream);
				Image musicCover1 =  GraphicsUtil.resize(musicCover, 64,64);
				display.asyncExec(new Runnable() {
					
					@Override
					public void run() {
						ui.global.Operation.getMusicCover().setImage(musicCover1);
					}
				});
			}
		}.start();
		ui.global.Operation.getMusicInfo().initail(music);
		ui.global.Operation.getMusicInfo().layout();
		if (music.isFavorite()) {
			ui.global.Operation.getFavoriteButton().state = true;
			ui.global.Operation.getFavoriteButton().active();
		}else {
			ui.global.Operation.getFavoriteButton().state = false;
			ui.global.Operation.getFavoriteButton().deactive();
		}
		if (music.isDownload()) {
			ui.global.Operation.getDownloadButton().state = true;
			ui.global.Operation.getDownloadButton().active();
		}else {
			ui.global.Operation.getDownloadButton().state = false;
			ui.global.Operation.getDownloadButton().deactive();
		}
	}
	
	//播放当前列表的第index首歌 自己开线程
	public void play(int index) {
		if (index < 0 || playList.size() == 0) {
			return;
		}
		currentMusicIndex = index;
		SongClass music = playList.get(index);
		ui.global.Current.setCurrentMusic(music);
		loadMusicInfo(music);//加载歌曲信息
		ui.global.Operation.getPlay_pauseButton().active(true);;
		new Thread() {
			@Override
			public void run() {
				try {
					Music_Playing.start(music);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}.start();
		if (PlaylistItem.getCurrentPlay() != null) {
			PlaylistItem.getCurrentPlay().deactive();
		}
		((PlaylistItem)((listView.getChildren())[currentMusicIndex])).active();
	}
	
	//继续播放
	public void _continue() {
		if (currentMusicIndex != -1) {
			Music_Playing.continuePlay();
		}
	}
	
	//暂停
	public void pause() {
		if (currentMusicIndex != -1) {
			Music_Playing.pause();
		}
	}
	
	//停止播放音乐
	public void stop() {
		//TODO
		try {
			Music_Playing.closeMusic();
			ui.global.Operation.getPlayProgressBar().progress = 0.0;
			ui.global.Operation.getPlayProgressBar().redraw();
			ui.global.Operation.getMusicInfo().clear();
			ui.global.Operation.getMusicCover().setImage(null);
			ui.global.Operation.getPlay_pauseButton().active(false);
			PlaylistItem.setCurrentPlay(null);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	//下一首歌
	public void nextMusic() {
		if (currentMusicIndex < 0) {
			return;
		}
		play(currentMusicIndex = (currentMusicIndex + 1) % playList.size());
	}
	
	//上一首歌
	public void previous() {
		if (currentMusicIndex < 0) {
			return;
		}
		if (currentMusicIndex - 1 < 0) {
			play(currentMusicIndex = playList.size() - 1);
		}else {
			play(currentMusicIndex -= 1);
		}
	}
	
	public List<SongClass> getPlayList() {
		return playList;
	}
	
	public void setCurrentMusic(int index) {
		this.currentMusicIndex = index;
	}
	
	public int getCurrentMusicIndex() {
		return currentMusicIndex;
	}
	//新建一个播放列表视图,相比与清空这样更有效率
	private void newListView() {
		if (listView != null) {
			listView.dispose();
		}
		listView = new ScrolledView(this, SWT.NONE);
		listView.setBounds(0, 100, 350, 600);
		listView.setScrollHeight(60);
	}
	
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public PlayList(Composite parent, int style) {
		super(parent, style);
		display = getDisplay();
		setSize(350, 800);
		newListView();
		
		Button playlistButton = new Button(this, SWT.NONE) {
			@Override
			public void mouseUp(MouseEvent e) {
				new Thread() {
					public void run() {
						for(int i = 0; i <= 350; i += 35) {
							int t = i;
							display.asyncExec(new Runnable() {			
								@Override
								public void run() {
									getParent().setLocation(930 + t, 0);
								}
							});
							try {
								Thread.sleep(10L);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
					}
				}.start();
			}
		};
		playlistButton.initial(
				SWTResourceManager.getImage(PlayList.class, "/image/playlist_button_deactive.png"),
				SWTResourceManager.getImage(PlayList.class, "/image/playlist_button_active.png"));
		playlistButton.setImage(SWTResourceManager.getImage(PlayList.class, "/image/playlist_button_deactive.png"));
		playlistButton.setBounds(292, 746, 32, 32);
		
		Label title = new Label(this, SWT.NONE);
		title.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		title.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 28, SWT.NORMAL));
		title.setBounds(10, 10, 204, 56);
		title.setText("\u64AD\u653E\u961F\u5217");
		
		amountView = new Label(this, SWT.NONE);
		amountView.setForeground(SWTResourceManager.getColor(SWT.COLOR_GRAY));
		amountView.setBounds(20, 72, 110, 17);
		amountView.setText("0\u9996\u6B4C\u66F2");
		
		Button clear = new Button(this, SWT.NONE) {
			
			@Override
			public void mouseUp(MouseEvent e) {
				playList = new ArrayList<>();
				initail(playList);
				stop();
			}
		};
		clear.setImage(SWTResourceManager.getImage(PlayList.class, "/image/delete_x32_deactive.png"));
		clear.setBounds(292, 57, 32, 32);
		clear.initial(
				SWTResourceManager.getImage(PlayList.class, "/image/delete_x32_deactive.png"),
				SWTResourceManager.getImage(PlayList.class, "/image/delete_x32_active.png"));
		
	}

	@Override
	protected void checkSubclass() {
		
	}
	@Override
	public void run() {
		for(int i = 0; i <= 350; i += 35) {
			int t = i;
			display.asyncExec(new Runnable() {			
				@Override
				public void run() {
					setLocation(1280 - t, 0);
				}
			});
			try {
				Thread.sleep(10L);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	public void pop() {
		new Thread(this).start();
	}
}
